const { Router } = require('express')
const express = require('express')
//路由容器
const add = express.Router()
const axios = require('axios')
const multipart = require('connect-multiparty');
const FormData = require('form-data')
var multipartMiddleware = multipart()
const fs = require("fs");
const { JsonWebTokenError } = require('jsonwebtoken')
const jwt = require("jsonwebtoken");
const { CONNREFUSED } = require('dns');
const { json } = require('body-parser');

var XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest;

add.post('/bussiness/uploadGood',multipartMiddleware, (req, res) => {
// add.post('/bussiness/goods',multipartMiddleware, (req, res) => {
    // console.log(req.body.params)
    // let {userId}=req.body.params
        let file = req.files.multipartFile;
        // let files = req.body
        // let theFiles = req.query
        console.log("-------------------------------------------------------------")
        console.log(req.files.goodsImg)
        console.log("-------------------------------------------------------------")
        // console.log(req.body)
        console.log(req.files.fileUpload)
        console.log("-------------------------------------------------------------")
        console.log(req.files.fileUpload.length)
        var files = new Array()
        if(req.files.fileUpload.length == undefined && req.files.fileUpload!=undefined){
            files.push(req.files.fileUpload.path)
            files.map(Number)
        }else{
            for(let i=0;i<req.files.fileUpload.length;i++){
                console.log(req.files.fileUpload[i])
                files.push(req.files.fileUpload[i].path)
                files.map(Number)
            }
        }
        console.log(files)
                    // console.log("-------------------------------------------------------------")
                    // var covers = new Array()
                    // if(req.files.goodsImg.length == undefined && req.files.goodsImg!=undefined){
                    //     covers.push(req.files.goodsImg.path)
                    //     covers.map(Number)
                    // }else{
                    //     for(let j=0;j<req.files.goodsImg.length;j++){
                    //         console.log(req.files.goodsImg[j])
                    //         covers.push(req.files.goodsImg[j].path)
                    //         covers.map(Number)
                    //     }
                    // }
                    // console.log("-------------------------------------------------------------")
        // console.log(theFiles)
        // for(let i=0;i<files.files.length;i++){
        //     console.log(JSON.parse(JSON.parse(files.files[i])))
        // }
                // console.log(files)
        // console.log(JSON.stringify({files:req.body.files[0]}))
        // console.log(JSON.stringify(files.files))
        // console.log(JSON.stringify(files.files[0]))
        // console.log(file)
                    // console.log("-------------------------------------------------------------")
        // let thefiles = req.files.multipartFiles;
                        // let goodsName=req.body.goodsName;     //商品名字
                        // let goodsPrice=req.body.goodsPrice;   //商品价格
                        // let stock=req.body.stock;             //商品库存
                        // let size=req.body.size;           //商品尺寸
                        // let color=req.body.color;         //颜色分类
                        // let introduction=req.body.introduction;  //商品描述
                        // let specName=req.body.specName;            //类别名
                        // let specContent=req.body.specContent;      //类别内容
                        // let shopId=req.body.shopId;                //店铺ID
                        // let bigId = req.body.bigId;
                        // let brandId = req.body.brandId;
                        // let userId = req.body.userId;
                        // console.log("-------------------------------------------------------------")
                        // var Price = new Array()
                        // for(let i=0;i<goodsPrice.length;i++){
                        //     Price.push(goodsPrice[i])
                        //     Price.map(Number)
                        // }
                        
                        // // console.log(file);
                        // // console.log(thefiles);
                        // // let userId = req.query.userId;
                        // console.log(userId);
                        // // console.log(file.headers)
                        // const uri = req.files.multipartFile.path
                        // // const uris = thefiles.path
                        // let formData = new FormData();
                        // formData.append('image', fs.createReadStream(uri));
                        // formData.append('files',JSON.stringify(files))
                            //
                            // for(let i=0;i<req.files.fileUpload.length;i++){
                            //     formData.append('files',req.files.fileUpload[i].path)
                            // }
                            //
                        // formData.append('covers',JSON.stringify(covers))
                        // // formData.append('goodsName',goodsName);
                        // for(let a in goodsPrice){  
                        //     formData.append('price',goodsPrice[a])
                        // }
                        // // formData.append('price',JSON.stringify(goodsPrice));
                        // for(let a in stock){  
                        //     formData.append('stock',stock[a])
                        // }
                        // // formData.append('stock',JSON.stringify(stock));
                        // for(let a in size){  
                        //     formData.append('size',size[a])
                        // }
                        // // formData.append('size',JSON.stringify(size));
                        // for(let a in color){  
                        //     formData.append('color',color[a])
                        // }
                        // // formData.append('color',JSON.stringify(color));
                        // formData.append('introduction',introduction);
                        // for(let a in specName){  
                        //     formData.append('specName',specName[a])
                        // }
                        // // formData.append('specName',JSON.stringify(specName));
                        // for(let a in specContent){  
                        //     formData.append('specContent',specContent[a])
                        // }
                        // // formData.append('specContent',JSON.stringify(specContent));
                        // // formData.append('shopId',shopId);
                        // // formData.append('time',time);
                        // // formData.append('bigId',bigId);
                        // // formData.append('brandId',brandId);
                        // // formData.append('userId',userId);
                        // // formData.append('specName',JSON.stringify(specName))
                        // console.log(formData)
                        // console.log('111');
        // console.log(formData)
// ----------------------------------------------------------------------------------------------------//
        // axios({
        //     method:"post",
        //     // url:`http://www.mianded.top:8081/bussiness/uploadGood/goodsName${encodeURI(
        //     //     goodsName
        //     // )}bigId${encodeURI(  
        //     //     bigId
        //     // )}userId${encodeURI(
        //     //     userId
        //     // )}brandId${encodeURI(
        //     //     brandId
        //     // )}shopId${encodeURI(
        //     //     shopId
        //     // )}introduction${encodeURI(
        //     //     introduction
        //     // )} 
        //     // `,
        //     url:`http://www.mianded.top:8081/bussiness/uploadGood/2/1/1/1/1/1`,
        //     // &color=${encodeURI(
        //     //     req.body.color
        //     // )}&price=${encodeURI(
        //     //     req.body.goodsPrice
        //     // )}&size=${encodeURI(
        //     //     req.body.size
        //     // )}&specContent=${encodeURI(
        //     //     req.body.specContent
        //     // )}&specName=${encodeURI(
        //     //     req.body.specName
        //     // )}&stock=${encodeURI(
        //     //     req.body.stock
        //     // )}
        //     // data: formData.getBuffer(),
        //     data: formData,
        //     header: formData.getHeaders(),
        //     contentType:"application/x-www-form-urlencoded",
        //     maxContentLength:Infinity,
        //     maxRedirects:5,
        //     maxBodyLength:Infinity,
        // })
        // .then(function(data){
        //     console.log("success============")
        //     console.log(data)
        //     res.send({data: data.data})
        // })
        // .catch((error) => {
        //     console.log(error.response);
        //     console.log(error.response.status);
        //     res.send(error)
        // })
// ----------------------------------------------------------------------------------------------------//
        var formData = new FormData();
        for(let i=0;i<req.files.fileUpload.length;i++){
            var uri = req.files.fileUpload[i].path
            formData.append('files',fs.createReadStream(uri))
        }
        console.log(formData)
        // formData.append("files",files)
        // console.log(files)
        axios({
            method:"post",
            url:`http://www.mianded.top:8081/bussiness/testUpload`,
            data: formData,
            header: formData.getHeaders(),
            contentType:"application/x-www-form-urlencoded",
            maxContentLength:Infinity,
            maxRedirects:5,
            maxBodyLength:Infinity,
        })
        .then(function(data){
            console.log("success============")
            // console.log(data)
            res.send({data: data.data})
        })
        .catch((error) => {
            console.log(error.response);
            console.log(error.response.status);
            res.send(error)
        })
// ----------------------------------------------------------------------------------------------------//
    // var xhr=new XMLHttpRequest();
    // var formData = new FormData();
    // for(let i=0;i<req.files.fileUpload.length;i++){
    //     formData.append('files',req.files.fileUpload[i].path)
    // }
    // var buffer = formData.getBuffer()
    // xhr.open("post","http://www.mianded.top:8081/bussiness/testUpload");
    // xhr.setRequestHeader ('Content-type', 'application/x-www-form-urlencoded');
    // xhr.onload = function() {
    //     console.log(xhr.responseText);
    // }
    // xhr.send(buffer);
    // xhr.onload=function(){
    //     if(xhr.status==200){
    //         console.log("成功了");
    //     }else{
    //         console.log("不写了");
    //     }
    // }
// ----------------------------------------------------------------------------------------------------//
})

module.exports=add;